package com.cs.base.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @ClassName: ArcProgressBar
 * @Date: 2021/10/22
 * @Author: CS
 * @Description: 弧形进度条
 */
public class ArcProgressBar extends View {
    //角度
    private float progress = 100f;
    //背景画笔
    private Paint panBg = new Paint();
    //圆弧画笔
    private Paint panArc = new Paint();
    //文字画笔
    private Paint textPan = new Paint();
    //颜色渐变器
    private SweepGradient sg = null;

    private float MAX_CIRCLE = 270f;

    private float ROTATE = 135f;

    private float percentStep = MAX_CIRCLE/100;

    private float backgroundStrokeWidth = 0f;

    private float progressStrokeWidth = 0f;

    public ArcProgressBar(Context context) {
        super(context);
        init();
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init(){
        //画笔抗锯齿
        textPan.setAntiAlias(true);
        panArc.setAntiAlias(true);
        //圆弧两端是圆弧状
        panArc.setStrokeCap(Paint.Cap.ROUND);
        //画笔样式为线
        panArc.setStyle(Paint.Style.STROKE);

        panBg.setAntiAlias(true);
        //圆弧两端是圆弧状
        panBg.setStrokeCap(Paint.Cap.ROUND);
        //画笔样式为线
        panBg.setStyle(Paint.Style.STROKE);

        //文字居中
        textPan.setTextAlign(Paint.Align.CENTER);

//        backgroundStrokeWidth = getWidth() / 27f;
//        progressStrokeWidth = getWidth() / 27f;
        //设置线宽
        backgroundStrokeWidth =  20f;
        progressStrokeWidth = 24f;
    }

    public void setBackgroundStrokeWidth(float width){
        backgroundStrokeWidth = width;
    }

    public void setProgressStrokeWidth(float width){
        progressStrokeWidth = width;
    }

    /**根据百分比处理圆弧*/
    public void setProgress(int percent) {
        progress = percent * percentStep;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //圆弧的宽度 可以根据你自己的需要调整 以下所有根据width/*皆为按比例适配不同大小的尺寸可以按需调整
        panArc.setStrokeWidth(progressStrokeWidth);
        panBg.setStrokeWidth(backgroundStrokeWidth);
        //文字大小为宽度的1/6
        textPan.setTextSize(getWidth() / 4.5f);
        textPan.setFakeBoldText(true);
        //旋转-90度让圆弧起点从右侧变为正上方
        canvas.rotate(ROTATE-percentStep*2, getWidth() / 2f, getWidth() / 2f);
        //渐变颜色实时获取以实时改变文字颜色 传入百分比起始颜色和结束颜色
        int color = getCurrentColor(progress / MAX_CIRCLE, Color.parseColor("#16B1FF"), Color.parseColor("#405CFD"));
        //设置文字颜色(渐变)

        textPan.setColor(Color.parseColor("#626262"));
        //设置渐变色的起始和终止颜色集合 可以设置多个
        int[] colors = {Color.parseColor("#00D06A"), Color.parseColor("#00D06A")};
        //将渐变色赋值给颜色渲染器
        if (sg == null) {
            sg = new SweepGradient(getWidth() / 2f, getWidth() / 2f, colors, null);
            panArc.setShader(sg);
        }
        panBg.setColor(Color.parseColor("#F0F0FF"));
        //绘制圆弧背景
        canvas.drawArc(20f, 20f, getWidth() - 20f, getWidth() - 20f, percentStep*2, MAX_CIRCLE, false, panBg);
        //绘制圆弧 此处像内侧偏移20px防止边框的圆弧无法完全展示 起始角度偏移 percentStep 度防止圆弧部分颜色变为结束颜色
        if(progress>0){
            canvas.drawArc(20f, 20f, getWidth() - 20f, getWidth() - 20f, percentStep*2,progress, false, panArc);
        }
        //回转90度 让文字正过来(因为刚刚旋转了-90度)
        canvas.rotate(-ROTATE+percentStep*2, getWidth() / 2f, getWidth() / 2f);
        //将角度换算为百分比
        int percent = (int)(progress * 100 / MAX_CIRCLE);
        //绘制文字
        canvas.drawText(percent+"%", getWidth() / 2f, getWidth() / 18f * 10f, textPan);
    }

    public int getCurrentColor(Float fraction, int startColor, int endColor) {
        int redCurrent;
        int blueCurrent;
        int greenCurrent;
        int alphaCurrent;
        int redStart = Color.red(startColor);
        int blueStart = Color.blue(startColor);
        int greenStart = Color.green(startColor);
        int alphaStart = Color.alpha(startColor);
        int redEnd = Color.red(endColor);
        int blueEnd = Color.blue(endColor);
        int greenEnd = Color.green(endColor);
        int alphaEnd = Color.alpha(endColor);
        int redDifference = redEnd - redStart;
        int blueDifference = blueEnd - blueStart;
        int greenDifference = greenEnd - greenStart;
        int alphaDifference = alphaEnd - alphaStart;
        redCurrent = (int)(redStart + fraction * redDifference);
        blueCurrent = (int)(blueStart + fraction * blueDifference);
        greenCurrent =  (int)(greenStart + fraction * greenDifference);
        alphaCurrent =  (int)(alphaStart + fraction * alphaDifference);
        return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent);
    }
}
